package com.me.study.javaCore.nio;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 测试 Path：
 *      1，相对路径以 `user.dir` 为相对目录，绝对路径从当前盘（卷）开始
 *      2，Junit、和 main() 运行时，`user.dir` 取值不一样
 */
public class PathTests {

    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("user.dir"));// D:\workspace\me\testME
        System.out.println(Paths.get("testJDK8/target/classes/test-file.txt").toUri());
        System.out.println(Paths.get("/home/joe/foo").toUri());
        System.out.println(Paths.get("C:/home/joe/foo").toUri());

        System.out.println();
        Path path = Paths.get("testJDK8/target/../target/classes/test-file.txt");
        System.out.format("toString: %s%n", path);
        // 返回真实路径（文件必须存在），去除了 冗余的  `./`    `../`  符号链接
        System.out.format("toRealPath: %s%n", path.toRealPath());
        // 返回绝对路径，不会去除 `../`
        System.out.format("toAbsolutePath: %s%n", path.toAbsolutePath());
        System.out.format("getFileName: %s%n", path.getFileName());
        System.out.format("getName(0): %s%n", path.getName(0));
        System.out.format("getNameCount: %d%n", path.getNameCount());
        // 相对路径、绝对路径区别对待
        System.out.format("subpath(0,2): %s%n", path.subpath(0,2));
        System.out.format("getParent: %s%n", path.getParent());
        // 相对路径返回 null
        System.out.format("getRoot: %s%n", path.getRoot());
    }

    @Test
    public void get() {
        System.out.println(System.getProperty("user.dir"));// D:\workspace\me\testME\testJDK8
        System.out.println(Paths.get("target/classes/test-file.txt").toUri());
        System.out.println(Paths.get("/home/joe/foo").toUri());
        System.out.println(Paths.get("C:/home/joe/foo").toUri());
    }

}
